#!/usr/bin/python3

import logging
from logging.handlers import RotatingFileHandler
import os


#定义日志的格式
FORMAT = "%(asctime)s [%(levelname)s]: %(message)s"
# LOGDIR = "/var/log/RevisionsManager/"
# LOGDIR = "/test666/"
        
class DuplicateFilter(logging.Filter):
    def __init__(self, threshold):
        super().__init__()
        self.last_logs = []
        self.repeat_count = {}
        self.threshold = threshold
    
    def filter(self, record):
        # current_log = (record.msg, record.args)  # 只有参数在变化，是确定不了是否重复的
        current_log = record.msg.split(",")[0]
        if current_log not in self.last_logs:
            self.last_logs.append(current_log)
            self.repeat_count[current_log] = 1
            if len(self.last_logs) > 5:
                oldest_log = self.last_logs.pop(0)
                del self.repeat_count[oldest_log]
        else:
            self.repeat_count[current_log] += 1
            
        if self.repeat_count[current_log] <= self.threshold:
            return True
        return False



class RMLog():
    def __init__(self) -> None:

        # loglevel = logging.INFO
        loglevel = logging.DEBUG
        # logFileName = get_logfile()
        logFileName = "/test666/test.log"
        logging.basicConfig(format=FORMAT, level=loglevel, datefmt='%m-%d,%H:%M:%S', 
                            handlers=[RotatingFileHandler(logFileName, maxBytes=20*1024, backupCount=5)])
        logging.getLogger().addFilter(DuplicateFilter(threshold=20))
        logging.info("RMLog init file[%s]", logFileName)



# 不覆盖日志文件，如果可以的话，那就不覆盖日志文件，只是把写完的日志文件压缩一下。
# 如果一定会覆盖日志文件，那就把日志文件的个数增加一下，保留尽可能多的日志，但是已经写完的日志，也需要压缩一下。
# 过滤器那里 是判断5条日志文件，放到一个列表中，如果，新写入的日志是这5条中的一条，并且重复数超过了阈值，那么就不再记录
# 什么时候更新这五条日志文件，每次都记录前五条日志，第六条在其中，count++,第六条不在其中，替换第5条为第六条，第4条为第五条...


def main():
    RMLog()
    count = 0
    logging.critical('[TEST2]: begin record')
    logging.critical('[TEST2]: begin record')
    logging.critical('[TEST2]: begin record')
    logging.critical('[TEST2]: begin record')
    while count < 60:
        logging.info('[TEST2]: this logging is a test info [%s]', count)
        logging.info('[TEST2]: this logging is a test1 info too, but it not a repeat info')
        logging.info('[TEST2]: this logging is a test2 info too, but it not a repeat info')
        logging.info('[TEST2]: this logging is a test3 info too, but it not a repeat info')
        logging.info('[TEST2]: this logging is a test4 info too, but it not a repeat info')
        count += 1

if __name__ == "__main__":
    main()
